/*
 *
 *  * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 *
 *
 */

package com.iwindplus.base.monitor;

import com.iwindplus.base.domain.constant.CommonConstant.ObservationConstant;
import com.iwindplus.base.monitor.domain.property.MonitorProperty;
import com.iwindplus.base.monitor.support.CustomObservationRegistryCustomizer;
import com.iwindplus.base.util.ObservationUtil;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.observation.ObservationRegistry;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.actuate.autoconfigure.observation.ObservationRegistryCustomizer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 监控配置.
 *
 * @author zengdegui
 * @since 2018/9/1
 */
@Slf4j
@Configuration
@EnableConfigurationProperties({MonitorProperty.class})
@ConditionalOnProperty(prefix = "monitor", name = "enabled", havingValue = "true", matchIfMissing = true)
public class MonitorConfiguration {

    @Value("${spring.application.name}")
    private String applicationName;

    @Resource
    private ObservationRegistry observationRegistry;

    /**
     * 创建 MeterRegistryCustomizer<MeterRegistry> （公共标签）.
     *
     * @return MeterRegistryCustomizer<MeterRegistry>
     */
    @Bean
    public MeterRegistryCustomizer<MeterRegistry> meterConfigurer() {
        MeterRegistryCustomizer<MeterRegistry> meterRegistryCustomizer =
            (registry) -> registry.config().commonTags(ObservationConstant.APPLICATION, applicationName);
        log.info("MeterRegistryCustomizer<MeterRegistry>={}", meterRegistryCustomizer);
        return meterRegistryCustomizer;
    }

    /**
     * 创建 ObservationRegistryCustomizer<ObservationRegistry>.
     *
     * @param monitorProperty 配置
     * @return ObservationRegistryCustomizer<ObservationRegistry>
     */
    @Bean
    public ObservationRegistryCustomizer<ObservationRegistry> observationConfigurer(MonitorProperty monitorProperty) {
        ObservationRegistryCustomizer<ObservationRegistry> observationRegistryCustomizer = new CustomObservationRegistryCustomizer()
            .customizer(monitorProperty);
        log.info("ObservationRegistryCustomizer<ObservationRegistry>={}", observationRegistryCustomizer);
        return observationRegistryCustomizer;
    }

    /**
     * 初始化 ObservationUtil.
     *
     * @return Object
     */
    @Bean("observationUtilInitializer")
    public Object observationUtilInitializer() {
        ObservationUtil.setObservationRegistry(observationRegistry);
        log.info("observationUtilInitializer");
        return "observationUtilInitializer";
    }
}
